package mlz.e_objectstream;

import org.junit.Test;

import java.io.*;
import java.util.Date;

/*
    序列化流
 */
public class ObjectStreamDemo {
    @Test
    public void test01() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        //写入JDK提供的类相关的内容
        Date date = new Date();
        //写入date时间
        oos.writeObject(date);


        //写入String对象
        String str = "hello";
        oos.writeObject(str);

        oos.writeObject(null);

        //关闭流对象
        oos.close();

    }

    @Test
    public void test02() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
        Date date = (Date) ois.readObject();
        System.out.println(date);

        String str = (String) ois.readObject();
        System.out.println(str);

        Object o = ois.readObject();
        System.out.println(o);

        /*读到文件末尾: EOFException */
        Object o1 = ois.readObject();
        System.out.println(o1);
    }
}








